package com.sky.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public PageResult List(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        Page<Category> categoryPage = categoryMapper.selectAll(categoryPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setRecords(categoryPage.getResult());
        pageResult.setTotal(categoryPage.getTotal());
        return pageResult;
    }

    @Override
    public void updateAll(Category category) {

        LambdaUpdateWrapper<Category> categoryLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        categoryLambdaUpdateWrapper.set(category.getType()!=null,Category::getType,category.getType())
                .set(category.getSort()!=null,Category::getSort,category.getSort())
                .set(category.getName()!=null,Category::getName,category.getName())
                .set(category.getStatus()!=null,Category::getStatus,category.getStatus())
                .set(category.getUpdateTime()!=null,Category::getUpdateTime,category.getUpdateTime())
                .set(category.getUpdateUser()!=null,Category::getUpdateUser,category.getUpdateUser())
                .eq(Category::getId,category.getId());
        update(categoryLambdaUpdateWrapper);
    }
}
